package com.meilai.project.mapper.media;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.dto.business.intentionScheme.PointProcessDateRangeDTO;
import com.meilai.project.entity.media.MediaPointOccupySnapshot;
import com.meilai.project.vo.business.installOrder.MediaPointLockDateRangeVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 点位占用快照 Mapper 接口
 * </p>
 *
 * @author 张驰
 * @since 2022-01-25
 */
@Mapper
public interface MediaPointOccupySnapshotMapper extends BaseMapper<MediaPointOccupySnapshot> {

    @Select("select DISTINCT mpos.media_point_id, mpos.choose_id, mpc.type, vcc.customer_id,vcc.customer_name, mpos.daterange_id, mpcd.start_at,mpcd.end_at " +
            "from media_point_occupy_snapshot mpos " +
            "left join v_choose_customer vcc on mpos.choose_id = vcc.choose_id " +
            "join media_point_choose mpc on mpc.id = mpos.choose_id  " +
            "join media_point_choose_daterange mpcd on mpos.daterange_id = mpcd.id and mpcd.end_at >= NOW() " +
            "where mpos.media_point_id = #{media_point_id} " +
            "order by mpcd.end_at desc")
    List<MediaPointLockDateRangeVO> listOccupyByMediaId(@Param("media_point_id") Long media_point_id);

    @Select("SELECT " +
            " mpos.id AS id " +
            "FROM " +
            " media_point_occupy_snapshot mpos " +
            " JOIN media_point_choose_daterange mpcd ON mpos.choose_id = mpcd.choose_id  " +
            " AND mpos.daterange_id = mpcd.id  " +
            " AND DATE_FORMAT(mpcd.end_at,'%Y-%m-%d') < DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 6 month),'%Y-%m-%d') ")
    List<Long> getExpireIds();

    List<String> queryLockSchemeNameListByMediaIds(@Param("pointIds") List<Long> pointIds);

    List<Long> occupyIds(@Param("chooseId") Long chooseId,
                         @Param("query") PointProcessDateRangeDTO query,
                         @Param("ids") List<Long> mediaPointIds);

    List<Long> occupyIdsExcludeChooseIds(@Param("chooseIdList") List<Long> chooseIdList,
                                         @Param("query") PointProcessDateRangeDTO query,
                                         @Param("ids") List<Long> mediaPointIds);

    @Delete("delete from media_point_occupy_snapshot where choose_id = #{chooseId} ")
    int deleteByChooseId(@Param("chooseId") Long chooseId);

    int deleteByChooseIdList(@Param("chooseIdList") List<Long> chooseIdList);

    int deleteBackUpByChooseIdList(@Param("chooseIdList") List<Long> chooseIdList);

    int backup(@Param("chooseIdList") List<Long> chooseIdList);

    int rollback(@Param("chooseIdList") List<Long> chooseIdList);

    @Delete("delete from media_point_occupy_snapshot where choose_id = #{chooseId} and daterange_id = #{dateRangeId} ")
    int deleteByChooseIdAndDateRangeId(@Param("chooseId") Long chooseId, @Param("dateRangeId") Long dateRangeId);

    /**
     * 大屏不占用
     */
    @Insert(" insert into media_point_occupy_snapshot(choose_id,daterange_id,media_point_id,media_type_spec_price_id ) " +
            " (select choose_id,daterange_id,media_point_id,media_type_spec_price_id " +
            " from media_point_choose_daterange_mediapoint mpcdm " +
            " join media_point mp on mpcdm.media_point_id = mp.id and mp.media_type_id != 11" +
            " where choose_id = #{chooseId}) ")
    int saveByChooseId(@Param("chooseId") Long chooseId);

    @Insert("insert into media_point_occupy_snapshot( " +
            "choose_id,daterange_id,media_point_id,media_type_spec_price_id) " +
            "(select #{new_choose_id},#{new_daterange_id},media_point_id,media_type_spec_price_id from media_point_occupy_snapshot " +
            "where choose_id = #{old_choose_id} and daterange_id = #{old_daterange_id}) ")
    int copyOldToNew(@Param("new_choose_id") Long new_choose_id, @Param("new_daterange_id") Long new_daterange_id,
                     @Param("old_choose_id") Long old_choose_id, @Param("old_daterange_id") Long old_daterange_id);

    List<Long> getLockPointIdsByIds(@Param("ids") List<Long> ids);
}
